<?php

namespace app\admin\controller\kuyou;

use app\common\controller\Backend;
use think\Db;
use app\admin\model\User;
use app\admin\model\Admin;
use app\common\model\Attachment;
use fast\Date;

/**
 * 数据管理
 *
 * @icon fa fa-circle-o
 */
class Datainfo extends Backend
{
    /**
     * Accounts模型对象
     * @var \app\admin\model\kuyou\Accounts
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $setData = (new Goods())->getSetData();
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set', $setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $storesList = (new \app\admin\model\kuyou\Stores())->where('shop_id', $this->shop_id)->select();
        $storesLists = [];
        foreach ($storesList as $storesList_k => $storesList_v) {
            $storesLists[] = ['name' => $storesList_v['name'], 'value' => $storesList_v['id']];
        }
        $this->view->assign("storesList", $storesList);
        $this->assignconfig("storesList", $storesLists);

        $storesList = (new \app\admin\model\kuyou\GoodsType())->where('shop_id', $this->shop_id)->select();
        $storesLists = [];
        foreach ($storesList as $storesList_k => $storesList_v) {
            $storesLists[] = ['name' => $storesList_v['supplier_type_name'], 'value' => $storesList_v['id']];
        }
        $this->assignconfig("goodsList", $storesLists);
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            try {
                \think\Db::execute("SET @@sql_mode='';");
            } catch (\Exception $e) {

            }
            $column = [];
            $starttime = Date::unixtime('day', -6);
            $endtime = Date::unixtime('day', 0, 'end');

            $types = $this->request->post("types");
            $legend = '购货金额';
            $joinlist = Db("kuyou_goods_purchase")
                ->where('shop_id', $this->shop_id)
                ->where('purchase_time', 'between time', [$starttime, $endtime])
                ->where(['examine_status' => '1'])
                ->field('SUM(real_money) AS nums,DATE_FORMAT(FROM_UNIXTIME(purchase_time), "%Y-%m-%d") AS join_date')
                ->group('join_date')
                ->select();
            if ($types == 2) {
                $joinlist = Db("kuyou_goods_sales")
                    ->where('shop_id', $this->shop_id)
                    ->where('purchase_time', 'between time', [$starttime, $endtime])
                    ->where(['examine_status' => '1'])
                    ->field('SUM(real_money) AS nums,DATE_FORMAT(FROM_UNIXTIME(purchase_time), "%Y-%m-%d") AS join_date')
                    ->group('join_date')
                    ->select();
                $legend = '销货金额';
            } elseif ($types == 3) {
                $list = Db::name('kuyou_stores')
                    ->where('shop_id', $this->shop_id)
                    ->where(['status' => '1'])
                    ->field('id,name')
                    ->select();
                foreach ($list as $list_k => $list_v) {
                    $list[$list_k]['value'] = Db::name('kuyou_goods_stock')
                        ->where('shop_id', $this->shop_id)
                        ->where(['stores_id' => $list_v['id']])
                        ->sum('stock');
                    if (isset($list[$list_k]['id'])) {
                        unset($list[$list_k]['id']);
                    }
                }
                $result = array('title' => '库存统计', 'legend' => '库存数量', "data" => $list);
                return json($result);
            } elseif ($types == 4) {
                $list = Db::name('kuyou_accounts')
                    ->where('shop_id', $this->shop_id)
                    ->field('account_name as name,balance as value')
                    ->select();
                $result = array('title' => '账户统计', 'legend' => '资金', "data" => $list);
                return json($result);
            }
            for ($time = $starttime; $time <= $endtime;) {
                $column[] = date("Y-m-d", $time);
                $time += 86400;
            }
            $list = array_fill_keys($column, 0);
            foreach ($joinlist as $k => $v) {
                $list[$v['join_date']] = $v['nums'];
            }
            foreach ($list as $list_k => $list_v) {
                if (empty($list_k)) {
                    unset($list[$list_k]);
                }
            }
            $result = array("legend" => $legend, "column" => array_keys($list), "data" => array_values($list));
            return json($result);
        }

        $time_arr = $this->getTimestamp('nowmonth');
        $code = Db::name('kuyou_goods_purchase')->where('shop_id', $this->shop_id)->where(['examine_status' => '1'])->where(['purchase_time' => ['BETWEEN', [$time_arr['start'], $time_arr['end']]]])->column('purchase_code');
        $purchase_num = Db::name('kuyou_goods_purchase_info')->where('shop_id', $this->shop_id)->where(['purchase_code' => ['in', $code]])->sum('basic_nmber');
        $purchase_mon = Db::name('kuyou_goods_purchase')->where('shop_id', $this->shop_id)->where(['purchase_code' => ['in', $code]])->sum('real_money');
        $this->view->assign('purchase_num', round($purchase_num, 2));
        $this->view->assign('purchase_mon', round($purchase_mon, 2));

        $code = Db::name('kuyou_goods_sales')->where('shop_id', $this->shop_id)->where(['examine_status' => '1'])->where(['purchase_time' => ['BETWEEN', [$time_arr['start'], $time_arr['end']]]])->column('purchase_code');
        $sales_num = Db::name('kuyou_goods_sales_info')->where('shop_id', $this->shop_id)->where(['purchase_code' => ['in', $code]])->sum('basic_nmber');
        $sales_mon = Db::name('kuyou_goods_sales')->where('shop_id', $this->shop_id)->where(['purchase_code' => ['in', $code]])->sum('real_money');
        $this->view->assign('sales_num', round($sales_num, 2));
        $this->view->assign('sales_mon', round($sales_mon, 2));

        $stock_num = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['status' => 1])->sum('inventory');
        $this->view->assign('stock_num', round($stock_num, 2));
        $goods = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['status' => 1])->column('goods_code');
        $stock_mon = 0;


        $sort = 'purchase_time';
        $order = 'ASC';
        $checkdate = Db::name('kuyou_checkout')->where('shop_id', $this->shop_id)->order(['checkdate' => 'DESC'])->value('checktime');
        $map = [];
        if (!empty($checkdate)) {
            $map['purchase_time'] = ['>', $checkdate];
        }
        $lists = Db::name('kuyou_goods_sales_profit')
            ->where('shop_id', $this->shop_id)
            ->where($map)
            ->order($sort, $order)
            ->select();

        $where = [];
        $list = Db::name('kuyou_goods_sales_profit')
            ->where('shop_id', $this->shop_id)
            ->where($where)
            ->order($sort, $order)
            ->select();

        $price_decimal = \think\Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere(['name' => 'price_decimal']))->value('value');
        $all_money_all = 0;
        $profit_all = 0;
        $net_profit_add = 0;

        $valuation_set = \think\Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'valuation'])->value('value');//计算方法
        $stores_set = \think\Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'stores'])->value('is_checked');//是否分仓
        if ($valuation_set != 2) {
            $valuation_set = 1;
        }
        if ($stores_set != 1) {
            $stores_set = 0;
        }

        $goods_info = [];
        foreach ($lists as $list_k => $list_v) {
            $cost_all = 0;
            $pay_money = 0;
            $info = Db::name('kuyou_goods_sales_info')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['purchase_code']])->field('pay_money,basic_nmber,goods_code,stores_id,purchase_time')->select();
            foreach ($info as $info_k => $info_v) {

                if (!isset($goods_info[$info_v['goods_code']])) {
                    $where = [];
                    $stores_ids = \think\Db::name('kuyou_stores')->where('shop_id', $this->shop_id)->where(['status' => '1'])->column('id');
                    if ($stores_set == 1) {
                        $where['stores_id'] = ['in', $stores_ids];
                    } else {
                        $where['stores_id'] = $info_v['stores_id'];
                    }
                    $where['goods_code'] = $info_v['goods_code'];

                    $checkdate = Db::name('kuyou_checkout')->where('shop_id', $this->shop_id)->order(['checkdate' => 'DESC'])->value('checktime');
                    if (empty($checkdate)) {
                        $enable_time = Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where(['name' => 'enable_time'])->value('value');
                        $start_time = strtotime($enable_time);

                        $end_time = strtotime(date('Y-m-d', $info_v['purchase_time']) . ' 23:59:59');
                        $wheretime['start_time'] = $start_time;
                        $wheretime['end_time'] = $end_time;
                        if (!empty($wheretime)) {
                            $where['purchase_time'] = ['between', [$wheretime['start_time'], time()]];
                        }
                        if ($valuation_set == 1) {
                            $purchase_info = Db::name('kuyou_goods_purchase_info')->where('shop_id', $this->shop_id)
                                ->where($where)
                                ->group('purchase_time')
                                ->field('purchase_time,sum(basic_nmber) as basic_nmber,sum(all_money) as all_money')
                                ->order(['purchase_time' => 'ASC'])->select();
                        } else {
                            $purchase_info = Db::name('kuyou_goods_purchase_info')
                                ->where('shop_id', $this->shop_id)
                                ->where($where)
                                ->field('purchase_time,basic_nmber,all_money')
                                ->order(['purchase_time' => 'ASC', 'purchase_code' => 'ASC'])
                                ->select();
                        }
                    } else {
                        $checkout_id = Db::name('kuyou_checkout')->where('shop_id', $this->shop_id)->order(['checkdate' => 'DESC'])->value('id');
                        $purchase_more = Db::name('kuyou_checkout_info')
                            ->where('shop_id', $this->shop_id)
                            ->where([
                                'checkout_id' => $checkout_id,
                                'goods_code' => $info_v['goods_code'],
                            ])->find();
                        $purchase_infos = Db::name('kuyou_goods_purchase')
                            ->where('shop_id', $this->shop_id)
                            ->where(['id' => $purchase_more['purchase_id']])
                            ->find();

                        $start_time = $checkdate;
                        $end_time = strtotime(date('Y-m-d', $info_v['purchase_time']) . ' 23:59:59');
                        $wheretime['start_time'] = $start_time;
                        $wheretime['end_time'] = $end_time;
                        if (!empty($wheretime)) {
                            $where['purchase_time'] = ['between', [$wheretime['start_time'], time()]];
                        }
                        if ($valuation_set == 1) {
                            $purchase_info = Db::name('kuyou_goods_purchase_info')
                                ->where('shop_id', $this->shop_id)
                                ->where($where)->group('purchase_time')
                                ->field('purchase_time,sum(basic_nmber) as basic_nmber,sum(all_money) as all_money')
                                ->order(['purchase_time' => 'ASC'])
                                ->select();
                            if (!empty($purchase_more)) {
                                foreach ($purchase_info as $purchase_info_k => $purchase_info_v) {
                                    if ($purchase_info_v['purchase_time'] == $purchase_more['purchase_time']) {
                                        $purchase_info[$purchase_info_k]['basic_nmber'] = (float)$purchase_more['av_basic_nmber'];
                                        $purchase_info[$purchase_info_k]['all_money'] = (float)$purchase_more['av_all_money'];
                                    }
                                }
                            }
                        } else {
                            if (empty($purchase_more)) {
                                $purchase_info = Db::name('kuyou_goods_purchase_info')
                                    ->where('shop_id', $this->shop_id)
                                    ->where($where)
                                    ->field('purchase_time,basic_nmber,all_money')
                                    ->order(['purchase_time' => 'ASC', 'purchase_code' => 'ASC'])->select();
                            } else {
                                $where['purchase_code'] = ['>=', $purchase_infos['purchase_code']];
                                $purchase_info = Db::name('kuyou_goods_purchase_info')
                                    ->where('shop_id', $this->shop_id)
                                    ->where($where)
                                    ->field('id,purchase_time,basic_nmber,all_money')
                                    ->order(['purchase_time' => 'ASC', 'purchase_code' => 'ASC'])
                                    ->select();
                                foreach ($purchase_info as $purchase_info_k => $purchase_info_v) {
                                    if ($purchase_info_v['id'] == $purchase_more['purchase_id']) {
                                        $purchase_info[$purchase_info_k]['basic_nmber'] = (float)$purchase_more['basic_nmber'];
                                        $purchase_info[$purchase_info_k]['all_money'] = (float)$purchase_more['all_money'];
                                    }
                                }
                            }
                        }
                    }

                    $where = [];
                    $where['goods_code'] = $info_v['goods_code'];
                    $where['stores_id'] = ['in', $stores_ids];
                    if (!empty($wheretime)) {
                        $where['createtime'] = ['between', [$wheretime['start_time'], time()]];
                    }
                    //期初
                    $period = Db::name('kuyou_goods_period')
                        ->where('shop_id', $this->shop_id)
                        ->where($where)
                        ->field('period as basic_nmber,price as all_money,createtime as purchase_time')
                        ->find();
                    if (!empty($period)) {
                        $period['purchase_time'] = strtotime(date('Y-m-d', $period['purchase_time']));
                        if ($valuation_set == 1) {
                            $status = true;
                            foreach ($purchase_info as $purchase_info_k => $purchase_info_v) {
                                if ($purchase_info_v['purchase_time'] == $period['purchase_time']) {
                                    $status = false;
                                    $purchase_info[$purchase_info_k]['basic_nmber'] += $period['basic_nmber'];
                                    $purchase_info[$purchase_info_k]['all_money'] += $period['all_money'];
                                }
                            }
                            if ($status) {
                                $purchase_info[] = $period;
                                $purchase_info = (new Goods())->arraySequence($purchase_info, 'purchase_time', 'SORT_ASC');
                            }
                        } else {
                            $purchase_infos = [];
                            $purchase_infos[] = $period;
                            foreach ($purchase_info as $k => $v) {
                                $purchase_infos[] = $v;
                            }
                            $purchase_info = $purchase_infos;
                        }
                    }

                    $goods = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['goods_code' => $info_v['goods_code']])->field('id,unit_id')->find();
                    if ($goods) {
                        $where = [];
                        $where['goods_id'] = $goods['id'];
                        $where['stores_id'] = ['in', $stores_ids];
                        if (!empty($wheretime)) {
                            $where['purchase_time'] = ['between', [$wheretime['start_time'], time()]];
                        }
                        //成本调整
                        $adjustment_money = 0;
                        $adjustment = \think\Db::name('kuyou_adjustment_info')->where('shop_id', $this->shop_id)->where($where)->select();
                        foreach ($adjustment as $adjustment_k => $adjustment_v) {
                            $advance_rate = 1;//单位换算进率
                            if ($adjustment_v['goods_unit'] != $goods['unit_id']) {
                                $unit = \think\Db::name('kuyou_unit_conversion')->where('shop_id', $this->shop_id)->where(['main_unit' => $goods['unit_id'], 'vice_unit' => $adjustment_v['goods_unit']])->value('advance_rate');
                                if (!empty($unit) && $unit > 0) {
                                    $advance_rate = $unit;
                                }
                            }
                            $money = $adjustment_v['money'] / $advance_rate;//换算后的调整成本
                            $adjustment[$adjustment_k]['purchase_time'] = strtotime(date('Y-m-d', $adjustment_v['purchase_time']));
                            $adjustment[$adjustment_k]['all_money'] = $adjustment_v['money'];
                        }
                        foreach ($adjustment as $adjustment_k => $adjustment_v) {
                            foreach ($purchase_info as $purchase_info_k => $purchase_info_v) {
                                if ($purchase_info_v['purchase_time'] == $adjustment_v['purchase_time']) {
                                    $purchase_info[$purchase_info_k]['all_money'] += $adjustment_v['all_money'];
                                }
                            }
                        }
                        //成本调整结束
                    }
                } else {
                    $purchase_info = $goods_info[$info_v['goods_code']];
                }

                $cost = (new Goods())->getCost_Price($info_v['goods_code'], '', $info_v['basic_nmber'], $purchase_info);

                if (isset($cost['purchase_info'])) {
                    $goods_info[$info_v['goods_code']] = $cost['purchase_info'];
                    $purchase_info = $goods_info[$info_v['goods_code']];
                    $cost = $cost['cost'];
                }
                $cost_all += $cost * $info_v['basic_nmber'];
                $pay_money += $info_v['pay_money'];
            }
            $lists[$list_k]['pay_money'] = $pay_money;
            if ($list_v['purchase_type'] == 2) {
                $cost_all = -$cost_all;
            }
            $lists[$list_k]['sales_cost'] = round($cost_all, $price_decimal);
        }
        $sales_cost = 0;
        foreach ($list as $list_k => $list_v) {
            $status = true;
            foreach ($lists as $lists_k => $lists_v) {
                if ($list_v['id'] == $lists_v['id']) {
                    $status = false;
                    $sales_cost += $lists[$lists_k]['sales_cost'];
                }
            }
            if ($status) {
                $sales_cost += $list_v['sales_cost'];
            }
        }
        $purchase_real_money = Db::name('kuyou_goods_purchase')->where('shop_id', $this->shop_id)->where(['examine_status' => '1'])->sum('real_money');
        $purchase_preferential_money = Db::name('kuyou_goods_purchase')->where('shop_id', $this->shop_id)->where(['examine_status' => '1'])->sum('preferential_money');
        $purchase_money = round($purchase_real_money + $purchase_preferential_money - $sales_cost, $price_decimal);
        $this->view->assign('purchase_money', $purchase_money);
        $this->view->assign('stock_mon', $sales_cost);

        $customers_mon = Db::name('kuyou_customers')->where('shop_id', $this->shop_id)->sum('need_receive_account');
        $suppliers_mon = Db::name('kuyou_suppliers')->where('shop_id', $this->shop_id)->sum('need_give_account');
        $this->view->assign('customers_mon', round($customers_mon, 2));
        $this->view->assign('suppliers_mon', round($suppliers_mon, 2));

        $accounts_type1 = Db::name('kuyou_accounts')->where('shop_id', $this->shop_id)->where(['account_type_id' => '1'])->sum('balance');
        $accounts_type2 = Db::name('kuyou_accounts')->where('shop_id', $this->shop_id)->where(['account_type_id' => '2'])->sum('balance');
        $this->view->assign('accounts_type1', round($accounts_type1, $price_decimal));
        $this->view->assign('accounts_type2', round($accounts_type2, $price_decimal));

        return $this->view->fetch();
    }

    public function datainfo()
    {
        if ($this->request->isAjax()) {
            //获取设置
            $set = \think\Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere())->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k => $set_v) {
                if (!empty($set_v['set'])) {
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal = $setList['price_decimal']['value'];

            $today = $this->getTimestamp();
            $start_time = $today['start'];
            $end_time = $today['end'];

            $sales_money = Db::name('kuyou_goods_sales')
                ->where('shop_id', $this->shop_id)
                ->where(['examine_status' => '1'])
                ->sum('money_tax_price');
            $sales_money = (int)$sales_money;
            $unit_ad = 10000;
            if ($sales_money < $unit_ad) {
                $unit = '元';
                $unit_status = 1;
            } else {
                $unit = '万元';
                $unit_status = 2;
            }
            $sales_money_str = array_map('intval', str_split($sales_money));
            $sales_money_str = array_reverse($sales_money_str);
            $a = 1;
            $num_data = [];
            foreach ($sales_money_str as $k => $v) {
                $num_data[] = $v;
                if ($a == 3) {
                    $num_data[] = ',';
                    $a = 0;
                }
                $a++;
            }
            if ($num_data[sizeof($num_data) - 1] == ',') {
                unset($num_data[sizeof($num_data) - 1]);
            }
            $num_data = array_reverse($num_data);

            //饼图
            $goods_type = Db::name('kuyou_goods_type')->where('shop_id', $this->shop_id)->where(['status' => '1'])->field('id,supplier_type_name as name')->select();
            foreach ($goods_type as $goods_type_k => $goods_type_v) {
                $goods_id = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['goods_type_id' => $goods_type_v['id'], 'status' => '1'])->column('goods_code');
                $this_sales_money = Db::name('kuyou_goods_sales_info')->alias('si')
                    ->join('kuyou_goods_sales s', 's.purchase_code=si.purchase_code')
                    ->where('shop_id', $this->shop_id)
                    ->where(['s.examine_status' => '1', 'si.goods_code' => ['in', $goods_id]])
                    ->sum('si.money_tax_price');
                $this_sales_money = round($this_sales_money, $price_decimal);
                if ($unit_status == 2) {
                    $this_sales_money = round($this_sales_money / $unit_ad, $price_decimal);
                }
                $goods_type[$goods_type_k]['sale_money'] = $this_sales_money;

                $this_sales_money = Db::name('kuyou_goods_sales_exchange_info')->alias('si')
                    ->join('kuyou_goods_sales_exchange s', 's.purchase_code=si.purchase_code')
                    ->where('shop_id', $this->shop_id)
                    ->where(['s.examine_status' => '1', 'si.goods_code' => ['in', $goods_id]])
                    ->sum('si.money_tax_price');
                $this_sales_money = round($this_sales_money, $price_decimal);
                if ($unit_status == 2) {
                    $this_sales_money = round($this_sales_money / $unit_ad, $price_decimal);
                }
                $goods_type[$goods_type_k]['exchange'] = $this_sales_money;
            }
            $left2_chart = [];
            foreach ($goods_type as $goods_type_k => $goods_type_v) {
                if ($goods_type_v['sale_money'] > 0) {
                    $left2_chart[] = ['value' => $goods_type_v['sale_money'], 'name' => $goods_type_v['name']];
                }
            }
            //退货
            $right2_chart = [];
            $max = 0;
            $all_money = 0;
            $right2_chart['yAxis'] = [];
            $right2_chart['series'] = [];
            $right2_chart['series']['max'] = [];
            $right2_chart['series']['data'] = [];
            foreach ($goods_type as $goods_type_k => $goods_type_v) {
                if ($goods_type_v['exchange'] > 0) {
                    $right2_chart['yAxis'][] = $goods_type_v['name'];
                    if ($max < $goods_type_v['exchange']) {
                        $max = $goods_type_v['exchange'];
                    }
                    $all_money += $goods_type_v['exchange'];
                }
            }
            foreach ($goods_type as $goods_type_k => $goods_type_v) {
                if ($goods_type_v['exchange'] > 0) {
                    $right2_chart['series']['data'][] = $goods_type_v['exchange'];
                    $right2_chart['series']['max'][] = $max;
                    if ($all_money != 0) {
                        if (isset($right2_chart['series']['per'][$goods_type_v['name']])) {
                            $right2_chart['series']['per'][$goods_type_v['name']] += $goods_type_v['exchange'];
                        } else {
                            $right2_chart['series']['per'][$goods_type_v['name']] = $goods_type_v['exchange'];
                        }
                    } else {
                        $right2_chart['series']['per'][$goods_type_v['name']] = $goods_type_v['exchange'];
                    }
                }
            }
            if (isset($right2_chart['series']['per'])) {
                foreach ($right2_chart['series']['per'] as $k => $v) {
                    if ($all_money != 0) {
                        $right2_chart['series']['per'][$k] = round($v / $all_money * 100, $price_decimal);
                    } else {
                        $right2_chart['series']['per'][$k] = 0;
                    }
                }
            }

            $mouth = (int)date('m');
            $time_arr_data = [];
            for ($a = 1; $a <= $mouth; $a++) {
                if ($a == 12) {
                    $time_arr_data[] = ['start_time' => strtotime(date('Y') . '-' . $a), 'end_time' => strtotime((date('Y') + 1) . '-01-01') - 1, 'time' => $a . '月'];
                } else {
                    $time_arr_data[] = ['start_time' => strtotime(date('Y') . '-' . $a), 'end_time' => strtotime(date('Y') . '-' . ($a + 1)) - 1, 'time' => $a . '月'];
                }
            }

            //新增客户
            $time_arr = $time_arr_data;
            $left3_chart = [];
            $xAxis = [];
            $series = [];
            $series['customers'] = [];
            $series['customers_num'] = [];
            foreach ($time_arr as $time_arr_k => $time_arr_v) {
                $xAxis[] = $time_arr_v['time'];
                $customers_num = Db::name('kuyou_customers')->where('shop_id', $this->shop_id)->where(['createtime' => ['<', $time_arr_v['end_time']]])->count();
                $customers = Db::name('kuyou_customers')->where('shop_id', $this->shop_id)->where(['createtime' => ['between', [$time_arr_v['start_time'], $time_arr_v['end_time']]]])->count();
                $series['customers'][] = $customers;
                $series['customers_num'][] = $customers_num;
            }
            $left3_chart['xAxis'] = $xAxis;
            $left3_chart['series'] = $series;

            //累计营业额
            $time_arr = $time_arr_data;
            $center1_chart = [];
            $xAxis = [];
            $series = [];
            $series['count'] = [];
            $series['num'] = [];
            foreach ($time_arr as $time_arr_k => $time_arr_v) {
                $xAxis[] = $time_arr_v['time'];
                $where = [];
                $where['purchase_time'] = ['between', [$time_arr_v['start_time'], $time_arr_v['end_time']]];
                $where['examine_status'] = '1';
                $this_sales_money = Db::name('kuyou_goods_sales')
                    ->where('shop_id', $this->shop_id)
                    ->where($where)
                    ->sum('money_tax_price');
                $this_sales_money = round($this_sales_money, $price_decimal);
                if ($unit_status == 2) {
                    $this_sales_money = round($this_sales_money / $unit_ad, $price_decimal);
                }
                $this_sales_count = Db::name('kuyou_goods_sales')->where('shop_id', $this->shop_id)->where($where)->count('id');
                $series['count'][] = $this_sales_count;
                $series['num'][] = $this_sales_money;
            }
            $center1_chart['xAxis'] = $xAxis;
            $center1_chart['series'] = $series;

            //采购/销售
            $time_arr = $time_arr_data;
            $center2_chart = [];
            $yAxis = [];
            $series = [];
            $series['purchase'] = [];
            $series['sales'] = [];
            foreach ($time_arr as $time_arr_k => $time_arr_v) {
                $yAxis[] = $time_arr_v['time'];

                $where = [];
                $where['purchase_time'] = ['between', [$time_arr_v['start_time'], $time_arr_v['end_time']]];
                $where['examine_status'] = '1';

                $this_sales_money = Db::name('kuyou_goods_purchase')
                    ->where('shop_id', $this->shop_id)
                    ->where($where)
                    ->field('sum(preferential_money) as preferential_money,sum(real_money) as real_money')->find();
                $this_sales_money = round($this_sales_money['preferential_money'] + $this_sales_money['real_money'], $price_decimal);
                if ($unit_status == 2) {
                    $this_sales_money = round($this_sales_money / $unit_ad, $price_decimal);
                }
                $series['purchase'][] = -$this_sales_money;
                $this_sales_money = Db::name('kuyou_goods_sales')
                    ->where('shop_id', $this->shop_id)
                    ->where($where)
                    ->sum('money_tax_price');
                $this_sales_money = round($this_sales_money, $price_decimal);
                if ($unit_status == 2) {
                    $this_sales_money = round($this_sales_money / $unit_ad, $price_decimal);
                }
                $series['sales'][] = $this_sales_money;
            }
            $center2_chart['yAxis'] = $yAxis;
            $center2_chart['series'] = $series;

            //销售排行
            $sale_list = Db::name('kuyou_goods_sales_info')->alias('si')
                ->join('kuyou_goods_sales s', 's.purchase_code=si.purchase_code')
                ->where('shop_id', $this->shop_id)
                ->where(['s.examine_status' => '1'])
                ->group('si.goods_code')
                ->field('si.goods_code,sum(si.money_tax_price) as money_tax_price')
                ->order(['money_tax_price' => 'DESC'])->select();
            foreach ($sale_list as $sale_list_k => $sale_list_v) {
                $sale_list[$sale_list_k]['goods_name'] = Db::name('kuyou_goods')->where('shop_id', $this->shop_id)->where(['goods_code' => $sale_list_v['goods_code']])->value('goods_name');
                $sale_list[$sale_list_k]['money_tax_price'] = round($sale_list_v['money_tax_price'], $price_decimal);
                if ($unit_status == 2) {
                    $sale_list[$sale_list_k]['money_tax_price'] = round($sale_list_v['money_tax_price'] / $unit_ad, $price_decimal);
                }
            }

            $result = array('unit' => $unit, "sales_money" => $num_data, "left2_chart" => $left2_chart, "left3_chart" => $left3_chart, 'center1_chart' => $center1_chart
            , 'center2_chart' => $center2_chart, 'right2_chart' => $right2_chart, 'sale_list' => $sale_list);
            return json($result);
        }
        $weekarray = array("日", "一", "二", "三", "四", "五", "六"); //先定义一个数组
        $week = "星期" . $weekarray[date("w")];
        $date = date('Y年m月d日');
        $this->view->assign("date", $date);
        $this->view->assign("week", $week);
        $this->view->assign("time", date('H:i:s'));
        $this->view->assign("title", '可视化大屏');
        return $this->view->fetch();
    }

    public function datainfos()
    {
        if ($this->request->isAjax()) {
            //获取设置
            $set = \think\Db::name('kuyou_sets')->where('shop_id', $this->shop_id)->where((new Goods())->setWhere())->field('type,name,value,is_checked,set')->select();
            $setList = [];
            foreach ($set as $set_k => $set_v) {
                if (!empty($set_v['set'])) {
                    $set_v['set'] = unserialize($set_v['set']);
                }
                $setList[$set_v['name']] = $set_v;
            }
            //数量价格格式化
            $number_decimal = $setList['number_decimal']['value'];
            $price_decimal = $setList['price_decimal']['value'];
            $today = $this->getTimestamp();
            $start_time = $today['start'];
            $end_time = $today['end'];

            $list = Db::name('kuyou_acceptance')
                ->where('shop_id', $this->shop_id)
                ->where(['purchase_time' => ['between', [$start_time, $end_time]], 'examine_status' => '1'])
                ->select();
            $works = [];
            $work_hours = 0;
            foreach ($list as $list_k => $list_v) {
                $list[$list_k]['hours'] = 0;
                if ($list_v['code_type'] == 1) {
                    //加工单
                    $production = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    if ($production) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $production['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number,goods_code')->select();
                        $hours = 0;
                        foreach ($goods_list as $goods_list_k => $goods_list_v) {
                            $bom_id = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where('shop_id', $this->shop_id)->where(['bom_id' => $bom_id])->select();
                            foreach ($procedure as $procedure_k => $procedure_v) {
                                $hours += $procedure_v['hours'];
                            }
                        }
                        $list[$list_k]['hours'] = round($hours, $number_decimal);
                    }
                } else {
                    //委外加工单
                    $plan = Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    $list[$list_k]['outsourcing_free'] = $plan['free_money'];
                    if ($plan) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $plan['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number,goods_code')->select();
                        $hours = 0;
                        foreach ($goods_list as $goods_list_k => $goods_list_v) {
                            $bom_id = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where('shop_id', $this->shop_id)->where(['bom_id' => $bom_id])->select();
                            foreach ($procedure as $procedure_k => $procedure_v) {
                                $hours += $procedure_v['hours'];
                            }
                        }
                        $list[$list_k]['hours'] = round($hours, $number_decimal);
                    }
                }
                if (!in_array($list_v['work_id'], $works)) {
                    $works[] = $list_v['work_id'];
                }
                $work_hours += $list[$list_k]['hours'];
            }

            $acceptance_id = Db::name('kuyou_acceptance')
                ->where('shop_id', $this->shop_id)
                ->where(['purchase_time' => ['between', [$start_time, $end_time]], 'examine_status' => '1'])
                ->column('id');
            $count = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => ['in', $acceptance_id]])
                ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
            $count['need_number'] = round($count['need_number'], $number_decimal);
            $count['number'] = round($count['number'], $number_decimal);
            $count['bad_number'] = round($count['bad_number'], $number_decimal);
            if ($count['need_number'] != 0) {
                $count['number_pr'] = round($count['number'] / $count['need_number'] * 100, $number_decimal);
                $count['bad_number_pr'] = round($count['bad_number'] / $count['need_number'] * 100, $number_decimal);
            }
            $count['works'] = sizeof($works);
            $count['hours'] = $work_hours;

            $tj = [];
            $tj['count'] = $count;

            $jgd = [];
            $list = Db::name('kuyou_acceptance')
                ->where('shop_id', $this->shop_id)
                ->where(['purchase_time' => ['between', [$start_time, $end_time]], 'examine_status' => '1', 'code_type' => '1'])
                ->select();
            $works = [];
            $work_hours = 0;
            $need_number = 0;
            $number = 0;
            $bad_number = 0;
            foreach ($list as $list_k => $list_v) {
                $this_count = Db::name('kuyou_acceptance_info')
                    ->where('shop_id', $this->shop_id)
                    ->where(['production_id' => $list_v['id']])
                    ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
                $need_number += $this_count['need_number'];
                $number += $this_count['number'];
                $bad_number += $this_count['bad_number'];
                $list[$list_k]['hours'] = 0;
                if ($list_v['code_type'] == 1) {
                    //加工单
                    $production = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    if ($production) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $production['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number,goods_code')->select();
                        $hours = 0;
                        foreach ($goods_list as $goods_list_k => $goods_list_v) {
                            $bom_id = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where('shop_id', $this->shop_id)->where(['bom_id' => $bom_id])->select();
                            foreach ($procedure as $procedure_k => $procedure_v) {
                                $hours += $procedure_v['hours'];
                            }
                        }
                        $list[$list_k]['hours'] = round($hours, $number_decimal);
                    }
                } else {
                    //委外加工单
                    $plan = Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    $list[$list_k]['outsourcing_free'] = $plan['free_money'];
                    if ($plan) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $plan['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number,goods_code')->select();
                        $hours = 0;
                        foreach ($goods_list as $goods_list_k => $goods_list_v) {
                            $bom_id = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where('shop_id', $this->shop_id)->where(['bom_id' => $bom_id])->select();
                            foreach ($procedure as $procedure_k => $procedure_v) {
                                $hours += $procedure_v['hours'];
                            }
                        }
                        $list[$list_k]['hours'] = round($hours, $number_decimal);
                    }
                }
                if (!in_array($list_v['work_id'], $works)) {
                    $works[] = $list_v['work_id'];
                }
                $work_hours += $list[$list_k]['hours'];
            }
            $jgd['works'] = sizeof($works);
            $jgd['work_hours'] = $work_hours;
            $jgd['need_number'] = $need_number;
            $jgd['number'] = $number;
            $jgd['bad_number'] = $bad_number;
            $jgd['need_number'] = round($jgd['need_number'], $number_decimal);
            $jgd['number'] = round($jgd['number'], $number_decimal);
            $jgd['bad_number'] = round($jgd['bad_number'], $number_decimal);
            if ($jgd['need_number'] != 0) {
                $jgd['number_pr'] = round($jgd['number'] / $jgd['need_number'] * 100, $number_decimal);
                $jgd['bad_number_pr'] = round($jgd['bad_number'] / $jgd['need_number'] * 100, $number_decimal);
            }
            $tj['jgd'] = $jgd;

            $wwjgd = [];
            $list = Db::name('kuyou_acceptance')
                ->where('shop_id', $this->shop_id)
                ->where(['purchase_time' => ['between', [$start_time, $end_time]], 'examine_status' => '1', 'code_type' => '2'])
                ->select();
            $works = [];
            $work_hours = 0;
            $need_number = 0;
            $number = 0;
            $bad_number = 0;
            foreach ($list as $list_k => $list_v) {
                $this_count = Db::name('kuyou_acceptance_info')
                    ->where('shop_id', $this->shop_id)
                    ->where(['production_id' => $list_v['id']])
                    ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
                $need_number += $this_count['need_number'];
                $number += $this_count['number'];
                $bad_number += $this_count['bad_number'];
                $list[$list_k]['hours'] = 0;
                if ($list_v['code_type'] == 1) {
                    //加工单
                    $production = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    if ($production) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $production['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number,goods_code')->select();
                        $hours = 0;
                        foreach ($goods_list as $goods_list_k => $goods_list_v) {
                            $bom_id = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where('shop_id', $this->shop_id)->where(['bom_id' => $bom_id])->select();
                            foreach ($procedure as $procedure_k => $procedure_v) {
                                $hours += $procedure_v['hours'];
                            }
                        }
                        $list[$list_k]['hours'] = round($hours, $number_decimal);
                    }
                } else {
                    //委外加工单
                    $plan = Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    $list[$list_k]['outsourcing_free'] = $plan['free_money'];
                    if ($plan) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $plan['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number,goods_code')->select();
                        $hours = 0;
                        foreach ($goods_list as $goods_list_k => $goods_list_v) {
                            $bom_id = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where('shop_id', $this->shop_id)->where(['bom_id' => $bom_id])->select();
                            foreach ($procedure as $procedure_k => $procedure_v) {
                                $hours += $procedure_v['hours'];
                            }
                        }
                        $list[$list_k]['hours'] = round($hours, $number_decimal);
                    }
                }
                if (!in_array($list_v['work_id'], $works)) {
                    $works[] = $list_v['work_id'];
                }
                $work_hours += $list[$list_k]['hours'];
            }
            $wwjgd['works'] = sizeof($works);
            $wwjgd['work_hours'] = $work_hours;
            $wwjgd['need_number'] = $need_number;
            $wwjgd['number'] = $number;
            $wwjgd['bad_number'] = $bad_number;
            $wwjgd['need_number'] = round($wwjgd['need_number'], $number_decimal);
            $wwjgd['number'] = round($wwjgd['number'], $number_decimal);
            $wwjgd['bad_number'] = round($wwjgd['bad_number'], $number_decimal);
            if ($wwjgd['need_number'] != 0) {
                $wwjgd['number_pr'] = round($wwjgd['number'] / $wwjgd['need_number'] * 100, $number_decimal);
                $wwjgd['bad_number_pr'] = round($wwjgd['bad_number'] / $wwjgd['need_number'] * 100, $number_decimal);
            }
            $tj['wwjgd'] = $wwjgd;

            //折线统计图
            $time_arr = [];
            $time_start = strtotime(date('Y-m-d')) - 86400 * 6;
            for ($a = 0; $a < 7; $a++) {
                $row['start'] = $time_start + 86400 * $a;
                $row['end'] = $time_start + 86400 * ($a + 1) - 1;
                $row['date'] = date('m-d', $row['start']);
                $time_arr[] = $row;
            }
            foreach ($time_arr as $time_arr_k => $time_arr_v) {
                $start_time = $time_arr_v['start'];
                $end_time = $time_arr_v['end'];
                $list = Db::name('kuyou_acceptance')
                    ->where('shop_id', $this->shop_id)
                    ->where(['purchase_time' => ['between', [$start_time, $end_time]], 'examine_status' => '1'])
                    ->select();
                $need_number = 0;
                $number = 0;
                $bad_number = 0;
                foreach ($list as $list_k => $list_v) {
                    $this_count = Db::name('kuyou_acceptance_info')
                        ->where('shop_id', $this->shop_id)
                        ->where(['production_id' => $list_v['id']])
                        ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
                    $need_number += $this_count['need_number'];
                    $number += $this_count['number'];
                    $bad_number += $this_count['bad_number'];
                }
                $time_arr[$time_arr_k]['need_number'] = $need_number;
                $time_arr[$time_arr_k]['number'] = $number;
                $time_arr[$time_arr_k]['bad_number'] = $bad_number;
            }
            $data = [];
            $xAxis = [];
            $series = [];
            $all_number = [];
            $number = [];
            $bad_number = [];
            foreach ($time_arr as $time_arr_k => $time_arr_v) {
                $xAxis[] = $time_arr_v['date'];
                $all_number[] = $time_arr_v['need_number'];
                $number[] = $time_arr_v['number'];
                $bad_number[] = $time_arr_v['bad_number'];
            }
            $series['all_number'] = $all_number;
            $series['number'] = $number;
            $series['bad_number'] = $bad_number;
            $data['xAxis'] = $xAxis;
            $data['series'] = $series;


            $list = Db::name('kuyou_acceptance')
                ->where('shop_id', $this->shop_id)
                ->where(['examine_status' => '1'])->order(['purchase_time' => 'DESC'])->limit(7)
                ->select();
            foreach ($list as $list_k => $list_v) {
                $this_count = Db::name('kuyou_acceptance_info')
                    ->where('shop_id', $this->shop_id)
                    ->where(['production_id' => $list_v['id']])
                    ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number')->find();
                $list_v['need_number'] = round($this_count['need_number'], $number_decimal);
                $list_v['number'] = round($this_count['number'], $number_decimal);
                $list_v['bad_number'] = round($this_count['bad_number'], $number_decimal);
                $list_v['number_pr'] = $list_v['bad_number_pr'] = 0;
                if ($list_v['need_number'] != 0) {
                    $list_v['number_pr'] = round($list_v['number'] / $list_v['need_number'] * 100, $number_decimal);
                    $list_v['bad_number_pr'] = round($list_v['bad_number'] / $list_v['need_number'] * 100, $number_decimal);
                }
                $list[$list_k] = $list_v;
            }

            $acceptance_id = Db::name('kuyou_acceptance')
                ->where('shop_id', $this->shop_id)
                ->where(['examine_status' => '1'])
                ->column('id');
            $lists = Db::name('kuyou_acceptance_info')
                ->where('shop_id', $this->shop_id)
                ->where(['production_id' => ['in', $acceptance_id]])
                ->limit(17)
                ->group('goods_id')
                ->field('sum(need_number) as need_number,sum(number) as number,sum(bad_number) as bad_number,goods_name')
                ->order(['number' => 'DESC'])
                ->select();

            $result = array("countArr" => $tj, "dataArr" => $data, "list" => $list, "lists" => $lists);
            return json($result);
        }
        $weekarray = array("日", "一", "二", "三", "四", "五", "六"); //先定义一个数组
        $week = "星期" . $weekarray[date("w")];
        $date = date('Y年m月d日');
        $this->view->assign("date", $date);
        $this->view->assign("week", $week);
        $this->view->assign("time", date('H:i:s'));
        $this->view->assign("title", '可视化大屏');
        return $this->view->fetch();
    }

    public function stockworing()
    {
        if ($this->request->isAjax()) {
            $data = input();
            $where = [];
            $where['stock_warning'] = '1';
            $map = [];
            if (isset($data['filter'])) {
                $filter = (array)json_decode($data['filter'], true);
                $filter = $filter ? $filter : [];
                if (!empty($filter['goods_code'])) {
                    $where['goods_code'] = ["like", '%' . $filter['goods_code'] . '%'];
                }
                if (!empty($filter['purchase_time'])) {
                    $str = explode(' - ', $filter['purchase_time']);
                    $where['purchase_time'] = ["BETWEEN", [strtotime($str[0]), strtotime($str[1])]];
                }
                if (!empty($filter['goods_name'])) {
                    $where['goods_name'] = ["like", '%' . $filter['goods_name'] . '%'];
                }
                if (!empty($filter['type_id'])) {
                    $where['goods_type_id'] = ["in", $filter['type_id']];
                }
                if (!empty($filter['stores_id'])) {
                    $map['sw.stores_id'] = ["in", $filter['stores_id']];
                }
            }

            $list = Db::name('kuyou_goods')
                ->where('shop_id', $this->shop_id)
                ->where($where)
                ->select();

            $datas = [];
            foreach ($list as $list_k => $list_v) {
                $unit = Db::name('kuyou_unit')->where('shop_id', $this->shop_id)->where(['id' => $list_v['unit_id']])->field('id,name')->find();
                $list[$list_k]['unit'] = $unit;
                if ($unit) {
                    $unit_name = $unit['name'];
                    $rowadvance_rate = '1';
                    if ($list_v['more_unit'] == 1) {
                        $more_unit = Db::name('kuyou_unit_conversion')->alias('uc')
                            ->join('kuyou_unit u', 'u.id=uc.vice_unit')
                            ->where('shop_id', $this->shop_id)
                            ->where(['uc.main_unit' => $unit['id']])
                            ->order('uc.advance_rate', 'ASC')
                            ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')
                            ->select();
                        foreach ($more_unit as $unit_k => $unit_v) {
                            $unit_name .= ',' . $unit_v['name'];
                            $rowadvance_rate .= ':' . $unit_v['advance_rate'];
                        }
                        $list[$list_k]['unitconversion'] = $unit_name . '(' . $rowadvance_rate . ')';
                    } else {
                        $list[$list_k]['unitconversion'] = '';
                    }
                }
                if (empty($unit)) {
                    $list[$list_k]['unit'] = ['name' => ''];
                }
                $list[$list_k]['goodstype'] = Db::name('kuyou_goods_type')->where('shop_id', $this->shop_id)->where(['id' => $list_v['goods_type_id']])->field('supplier_type_name')->find();
                $list[$list_k]['stores'] = Db::name('kuyou_stores')->where('shop_id', $this->shop_id)->where(['id' => $list_v['manager_warehouse_id']])->field('name')->find();
                $list[$list_k]['stock'] = $list_v['inventory'];
                if ($list_v['branch'] == 1) {
                    $lists = Db::name('kuyou_goods_stock_warning')->alias('sw')
                        ->join('kuyou_stores ks', 'ks.id=sw.stores_id')
                        ->where('shop_id', $this->shop_id)
                        ->where($map)->where(['sw.goods_code' => $list_v['goods_code']])
                        ->field('sw.stores_id,sw.min_stock,sw.max_stock,ks.name')->select();
                    foreach ($lists as $lists_k => $lists_v) {
                        $stock = Db::name('kuyou_goods_stock')->where('shop_id', $this->shop_id)->where(['goods_code' => $list_v['goods_code'], 'stores_id' => $lists_v['stores_id']])->value('stock');
                        $datas[] = [
                            'goodstype' => $list[$list_k]['goodstype'],
                            'goods_code' => $list_v['goods_code'],
                            'goods_name' => $list_v['goods_name'],
                            'goods_barcode' => $list_v['goods_barcode'],
                            'goods_image' => $list_v['goods_image'],
                            'more_unit' => $list[$list_k]['more_unit'],
                            'unit' => $list[$list_k]['unit'],
                            'unitconversion' => $list[$list_k]['unitconversion'],
                            'stores' => ['name' => $lists_v['name']],
                            'min_stock' => $lists_v['min_stock'],
                            'max_stock' => $lists_v['max_stock'],
                            'stock' => (float)$stock,
                        ];
                    }
                    unset($list[$list_k]);
                } else {
                    $list[$list_k]['stores'] = ['name' => '全部仓库'];
                }
            }
            foreach ($datas as $datas_k => $datas_v) {
                $list[] = $datas_v;
            }
            foreach ($list as $list_k => $list_v) {
                if ($list_v['stock'] >= $list_v['min_stock'] && $list_v['stock'] <= $list_v['max_stock']) {
                    unset($list[$list_k]);
                }
            }
            $result = array("total" => sizeof($list), "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    function getTimestamp($name = 'today', $param = '', $mouth = '')
    {
        $nowtimr = strtotime(date('Y-m-d H') . ':' . (date('i') - date('i') % 10));
        switch ($name) {
            case 'now':
                $timeamp['start'] = $nowtimr - 600;
                $timeamp['end'] = $nowtimr;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'ten':
                $timeamp['start'] = $nowtimr - 1200;
                $timeamp['end'] = $nowtimr - 600;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'twenty':
                $timeamp['start'] = $nowtimr - 1800;
                $timeamp['end'] = $nowtimr - 1200;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'thirty':
                $timeamp['start'] = $nowtimr - 2400;
                $timeamp['end'] = $nowtimr - 1800;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'forty':
                $timeamp['start'] = $nowtimr - 3000;
                $timeamp['end'] = $nowtimr - 2400;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'fifty':
                $timeamp['start'] = $nowtimr - 3600;
                $timeamp['end'] = $nowtimr - 3000;
                $timeamp['time'] = date('H:i', $timeamp['end']);
                break;
            case 'today'://今天
                $timeamp['time'] = date('m-d');
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day'))) > time() ? time() : strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
            case 'yesterday'://昨天
                $timeamp['time'] = date('m-d', strtotime('-1 day'));
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-1 day')));
                $timeamp['end'] = strtotime(date('Y-m-d'));
                break;
            case 'beforyesterday'://前天
                $timeamp['time'] = date('m-d', strtotime('-2 day'));
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-2 day')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('-1 day')));
                break;
            case 'threeday'://前3天
                $timeamp['time'] = date('m-d', strtotime('-3 day'));
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-3 day')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('-2 day')));
                break;
            case 'fourday'://前4天
                $timeamp['time'] = date('m-d', strtotime('-4 day'));
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-4 day')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('-3 day')));
                break;
            case 'fifday'://前5天
                $timeamp['time'] = date('m-d', strtotime('-5 day'));
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-5 day')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('-4 day')));
                break;
            case 'sixday'://前6天
                $timeamp['time'] = date('m-d', strtotime('-6 day'));
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-6 day')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('-5 day')));
                break;
            case 'beforweek'://本周
                $timeamp['start'] = strtotime(date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"))));
                $timeamp['end'] = strtotime(date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"))));
                break;
            case 'nowmonth'://本月
                $timeamp['start'] = strtotime(date('Y-m-01'));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day'))) > time() ? time() : strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
            case 'permonth'://上月
                $timeamp['start'] = strtotime(date('Y-m-01', strtotime('-1 month')));
                $timeamp['end'] = strtotime(date('Y-m-01'));
                break;
            case 'oldmonth'://前几月
                $timeamp['start'] = strtotime(date('Y-m-01', strtotime('-' . $mouth . ' month')));
                if ($mouth == 1) {
                    $timeamp['end'] = strtotime(date('Y-m-01'));
                } else {
                    $timeamp['end'] = strtotime(date('Y-m-01', strtotime('-' . ($mouth - 1) . ' month')));
                }
                break;
            case 'preweek'://上周 注意我们是从周一开始算
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-2 week Monday')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('-1 week Monday +1 day')));
                break;
            case 'nowweek'://本周
                $timeamp['start'] = strtotime(date('Y-m-d', strtotime('-1 week Monday')));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
            case 'preday'://30
                $timeamp['start'] = strtotime(date('Y-m-d'), strtotime($param . ' day'));
                $timeamp['end'] = strtotime(date('Y-m-d'));
                break;
            case 'nextday'://30
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d'), strtotime($param . ' day'));
                break;
            case 'preyear'://去年
                $timeamp['start'] = strtotime(date('Y-01-01', strtotime('-1 year')));
                $timeamp['end'] = strtotime(date('Y-12-31', strtotime('-1 year')));
                break;
            case 'nowyear'://今年
                $timeamp['start'] = strtotime(date('Y-01-01'));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day'))) > time() ? time() : strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
            case 'quarter'://季度
                $quarter = empty($param) ? ceil((date('n')) / 3) : $param;//获取当前季度
                $timeamp['start'] = mktime(0, 0, 0, $quarter * 3 - 2, 1, date('Y'));
                $timeamp['end'] = mktime(0, 0, 0, $quarter * 3 + 1, 1, date('Y')) > time() ? time() : mktime(0, 0, 0, $quarter * 3 + 1, 1, date('Y'));
                break;
            case 'lastquarter'://季度
                $quarter = empty($param) ? ceil((date('n')) / 3) : $param;//获取当前季度
                $quarter -= 1;
                if ($quarter > 0) {
                    $timeamp['start'] = mktime(0, 0, 0, $quarter * 3 - 2, 1, date('Y'));
                    $timeamp['end'] = mktime(0, 0, 0, $quarter * 3 + 1, 1, date('Y')) > time() ? time() : mktime(0, 0, 0, $quarter * 3 + 1, 1, date('Y'));
                } else {
                    $quarter = 4;
                    $timeamp['start'] = mktime(0, 0, 0, $quarter * 3 - 2, 1, (date('Y') - 1));
                    $timeamp['end'] = mktime(0, 0, 0, $quarter * 3 + 1, 1, (date('Y') - 1)) > time() ? time() : mktime(0, 0, 0, $quarter * 3 + 1, 1, (date('Y') - 1));
                }
                break;
            case 'lastyeayquarter'://去年同季度
                $quarter = empty($param) ? ceil((date('n')) / 3) : $param;//获取当前季度
                $timeamp['start'] = mktime(0, 0, 0, $quarter * 3 - 2, 1, (date('Y') - 1));
                $timeamp['end'] = mktime(0, 0, 0, $quarter * 3 + 1, 1, (date('Y') - 1)) > time() ? time() : mktime(0, 0, 0, $quarter * 3 + 1, 1, (date('Y') - 1));
                break;
            default:
                $timeamp['start'] = strtotime(date('Y-m-d'));
                $timeamp['end'] = strtotime(date('Y-m-d', strtotime('+1 day')));
                break;
        }
        if ($timeamp['end'] > time()) {
            $timeamp['end'] = time();
        }
        return $timeamp;
    }
    
}
